package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.sumpu.app.guoguojie.dao.CmsContentDao;
import cn.sumpu.app.guoguojie.domain.CmsContent;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class CmsContentDaoImpl extends SqlSessionDaoSupport implements
		CmsContentDao {

	private static Logger logger = LoggerFactory
			.getLogger(CmsContentDaoImpl.class);

	// 添加内容
	@Override
	public void addCmsContent(CmsContent cmsContent)
			throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.addCmsContent",
							cmsContent);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addCmsContent database failure.", e);
			}
			throw new AppBusinessException("addCmsContent database failure.", e);
		}
	}

	// 取得内容列表
	@SuppressWarnings("unchecked")
	@Override
	public List<CmsContent> getContentList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<CmsContent>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getContentList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentList database failure.", e);
			}
			throw new AppBusinessException("getContentList database failure.",
					e);
		}
	}

	// 根据频道集合取得内容Id集合
	@SuppressWarnings("unchecked")
	@Override
	public List<String> getContentIdList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<String>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getContentIdList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentIdList database failure.", e);
			}
			throw new AppBusinessException(
					"getContentIdList database failure.", e);
		}
	}

	// 更新内容信息
	@Override
	public void updateContentById(CmsContent cmsContent)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.updateContentById",
							cmsContent);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateContentById database failure.", e);
			}
			throw new AppBusinessException(
					"updateContentById database failure.", e);
		}
	}

	// 根据该频道下有多少条内容
	@Override
	public Integer getContentCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getContentCount",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentCount database failure.", e);
			}
			throw new AppBusinessException("getContentCount database failure.",
					e);
		}
	}

	// 根据内容Id取得这条内容的信息
	@Override
	public CmsContent getContentById(String identifier)
			throws AppBusinessException {
		try {
			return (CmsContent) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getContentById",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentById database failure.", e);
			}
			throw new AppBusinessException("getContentById database failure.",
					e);
		}
	}

	// 根据内容Id的集合删除内容
	@Override
	public void deleteContentByIdList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.deleteContentByIdList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteContentByIdList database failure.", e);
			}
			throw new AppBusinessException(
					"deleteContentByIdList database failure.", e);
		}
	}

	// 根据内容Id删除内容
	@Override
	public void deleteContentById(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.deleteContentById",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteContentById database failure.", e);
			}
			throw new AppBusinessException(
					"deleteContentById database failure.", e);
		}
	}

	// 维护编号(type：null.全频道维护 1.删除维护(-1) 2.排序上移(+1) 3.排序下移(-1))
	@Override
	public void updateContentSort(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.updateContentSort",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateContentSort database failure.", e);
			}
			throw new AppBusinessException(
					"updateContentSort database failure.", e);
		}
	}

	// 全部更新为已发布
	@Override
	public void updateContentAll(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.updateContentAll",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateContentAll database failure.", e);
			}
			throw new AppBusinessException(
					"updateContentAll database failure.", e);
		}
	}

	// 取得大于当前时间的预约内容
	@SuppressWarnings("unchecked")
	@Override
	public List<CmsContent> getContentByTime() throws AppBusinessException {
		try {
			return (List<CmsContent>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getContentByTime");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentByTime database failure.", e);
			}
			throw new AppBusinessException(
					"getContentByTime database failure.", e);
		}
	}

	// 取得后台的内容列表
	@SuppressWarnings("unchecked")
	@Override
	public List<CmsContent> getBackContentList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<CmsContent>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getBackContentList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBackContentList database failure.", e);
			}
			throw new AppBusinessException(
					"getBackContentList database failure.", e);
		}
	}

	// 按照时间预览取得内容列表
	@SuppressWarnings("unchecked")
	@Override
	public List<CmsContent> getContentListByTime(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<CmsContent>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getContentListByTime",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentListByTime database failure.", e);
			}
			throw new AppBusinessException(
					"getContentListByTime database failure.", e);
		}
	}

	// 编号对调
	@Override
	public void updateContentBySort(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.updateContentBySort",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateContentBySort database failure.", e);
			}
			throw new AppBusinessException(
					"updateContentBySort database failure.", e);
		}
	}

	// 查询这条微博有没有已经被推送
	@Override
	public int getTblIdentifierCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getTblIdentifierCount",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTblIdentifierCount database failure.", e);
			}
			throw new AppBusinessException(
					"getTblIdentifierCount database failure.", e);
		}
	}

	// 更改封面
	@Override
	public void updateTagCover(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.updateTagCover",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateTagCover database failure.", e);
			}
			throw new AppBusinessException("updateTagCover database failure.",
					e);
		}
	}

	// 首页的格子
	@SuppressWarnings("unchecked")
	@Override
	public List<CmsContent> getProductGrid(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getProductGrid",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getProductGrid database failure.", e);
			}
			throw new AppBusinessException("getProductGrid database failure.",
					e);
		}
	}
	//这个频道下的所有内容排序都+1
	@Override
	public void updateContentAddOne(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.updateContentAddOne",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateContentAddOne database failure.", e);
			}
			throw new AppBusinessException("updateContentAddOne database failure.",
					e);
		}
	}
	//查询爱心社活动
	@SuppressWarnings("unchecked")
	@Override
	public List<CmsContent> getLoveActivity(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getLoveActivity",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getLoveActivity database failure.", e);
			}
			throw new AppBusinessException("getLoveActivity database failure.",
					e);
		}
	}
	//根据频道ID查询这个频道下最小序号
	@Override
	public Integer getMinSortByChannel(String channel) throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getMinSortByChannel",
							channel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMinSortByChannel database failure.", e);
			}
			throw new AppBusinessException("getMinSortByChannel database failure.",
					e);
		}
	}

	@Override
	public Integer getMaxSortByChannel(String channel)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getMaxSortByChannel",
							channel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMaxSortByChannel database failure.", e);
			}
			throw new AppBusinessException("getMaxSortByChannel database failure.",
					e);
		}
	}
	
	@Override
    public CmsContent getContentByMap(Map<String, Object> map) throws AppBusinessException {
        try {
            return (CmsContent) getSqlSession()
                    .selectOne(
                            "cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getContentByMap",map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getContentByMap database failure.", e);
            }
            throw new AppBusinessException("getContentByMap database failure.",
                    e);
        }
    }
	
	
   // 取得帮淘内容列表
    @SuppressWarnings("unchecked")
    @Override
    public List<CmsContent> getAmoysList(Map<String, Object> map)  throws AppBusinessException {
        try {
            return (List<CmsContent>) getSqlSession()
                    .selectList(
                            "cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getAmoysList",
                            map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getAmoysList database failure.", e);
            }
            throw new AppBusinessException("getAmoysList database failure.",
                    e);
        }
    }
    //帮淘列表页面 
    @SuppressWarnings("unchecked")
    @Override
    public List<CmsContent> getCmsContentListForAmoy(Map<String, Object> map)  throws AppBusinessException {
        try {
            return (List<CmsContent>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getCmsContentListForAmoy",
                            map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getCmsContentListForAmoy database failure.", e);
            }
            throw new AppBusinessException("getCmsContentListForAmoy database failure.",
                    e);
        }
    }
    //数量
    @Override
    public int getCmsContentListForAmoyCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.CmsContentMapper.getCmsContentListForAmoyCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getCmsContentListForAmoyCount database failure.", e);
            }
            throw new AppBusinessException("getCmsContentListForAmoyCount database failure.", e);
        }
    }
    
}